package com.sinosoft.flow.config;

import com.sinosoft.flow.config.annotation.PhoneAnnotation;
import org.apache.commons.lang.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @Author: tangyongbo
 * @Description:
 * @Date: create in 2021/11/10 9:42
 */
public class PhoneValidation implements ConstraintValidator<PhoneAnnotation, String> {

    private static final Pattern PATTERN= Pattern.compile( "^1[345678]\\d{9}$");

    @Override
    public void initialize(PhoneAnnotation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(s)) {
            return false;
        } else {
            return PATTERN.matcher(s).matches();
        }
    }
}
